package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.CureRecordFileLogic;
import cn.topevery.garden.entity.dto.CureRecordFileDTO;
import cn.topevery.garden.entity.po.CureRecordFile;
import cn.topevery.garden.entity.query.CureRecordFileQuery;
import cn.topevery.garden.entity.vo.CureRecordFileVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 防止记录 附件表
 *
 * @author code-generate
 * @date 2022-02-18
 */
@Api(tags = "防止记录 附件表")
@RestController
@RequestMapping("/cureRecordFile")
public class CureRecordFileController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    CureRecordFileLogic cureRecordFileLogic;


    /**
     * 获取防止记录 附件表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取防止记录 附件表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<CureRecordFileVO>> listPage(@RequestBody CureRecordFileQuery query) {
        return JsonResult.ok(cureRecordFileLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 防止记录 附件表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody CureRecordFileDTO dto) {
        cureRecordFileLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, CureRecordFile.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<CureRecordFileVO> getInfoById(@RequestParam Integer id) {
        CureRecordFile entity = cureRecordFileLogic.getEntity(id);
        CureRecordFileVO vo= mapper.map(entity, CureRecordFileVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        cureRecordFileLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出防止记录 附件表信息")
    @GetMapping("/export")
    public void export(CureRecordFileQuery query, HttpServletResponse response) {
        List<CureRecordFileVO> list = cureRecordFileLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "防止记录 附件表", "sheet1", CureRecordFileVO.class, "防止记录 附件表", response);
    }

}
